import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class test {
    public static void main(String[] args) {
        ArrayList<Group> CLASS = new ArrayList<>();

        final int Exit = 1;
        final int OK = 0;

        ArrayList<aClass> RealCLASS = new ArrayList<>();

        aClass first = new aClass(1);
        aClass second = new aClass(2);
        RealCLASS.add(first);
        RealCLASS.add(second);

        Student[] arr = new Student[6];
        arr[0] = new Student("孟毓贺", "2023015479", first);
        arr[1] = new Student("陈碧玉", "2023015478", first);
        arr[2] = new Student("雷军", "2023015477", first);
        arr[3] = new Student("余胜军", "2023015476", first);
        arr[4] = new Student("麻花疼", "2023015475", first);
        arr[5] = new Student("码云", "2023015474", first);

        CLASS.add(new Group(1, arr[0], arr[1]));
        CLASS.add(new Group(2, arr[2], arr[3]));
        CLASS.add(new Group(3, arr[4], arr[5]));


        int s1 = OK;
        Scanner sc = new Scanner(System.in);
        do {
            System.out.println(
                    "1.管理班级信息" + "  " +
                            "2.管理学生信息" + "  " +
                            "3.管理小组信息" + "  " +
                            "4.随机点名" + "  " +
                            "0.退出");
            int a = sc.nextInt();
            switch (a) {
                case 1: {
                    System.out.println("1.新增班级" + " " + "2.删除班级" + " " + "3.查看班级信息");
                    int aa = sc.nextInt();
                    switch (aa) {
                        case 1: {
                            System.out.println("请输入新增的班级（数字）");
                            int tmp = sc.nextInt();
                            if (!JudgeClass(RealCLASS, tmp)) {
                                aClass t = new aClass(tmp);
                                RealCLASS.add(t);
                                System.out.println("添加成功");
                            } else {
                                System.out.println("班级已存在");
                            }
                            break;
                        }
                        case 2: {
                            System.out.println("请输入要删除的班级(数字)");
                            int tmp = sc.nextInt();
                            if (JudgeClass(RealCLASS, tmp)) {
                                RealCLASS.remove(tmp);
                                DeleteClass(arr, tmp);
                                System.out.println("删除成功");
                            } else {
                                System.out.println("班级不存在");
                            }
                            break;
                        }
                        case 3: {
                            for (int i = 0; i < RealCLASS.size(); i++) {
                                System.out.println("班级：" + RealCLASS.get(i).getaClass());
                            }
                            break;
                        }
                    }
                    break;
                }
                case 2: {
                    System.out.println("1.添加学生信息" + "  " +
                            "2.删除学生信息" + "  " +
                            "3.修改学生信息" + " " + "4.查看学生信息");
                    int b = sc.nextInt();
                    switch (b) {
                        case 1: {
                            //数组没有满
                            if (Judge(arr) != arr.length) {
                                for (int i = 0; i < arr.length; i++) {
                                    if (arr[i] == null) {
                                        arr[i] = new Student();
                                        System.out.println("请输入要添加学生的学号");
                                        String id = sc.next();
                                        if (JudgeOnly(arr, id)) {
                                            arr[i].setId(id);
                                            System.out.println("请输入要添加学生的名字");
                                            String name = sc.next();
                                            arr[i].setName(name);
                                            System.out.println("请输入要添加学生的班级");
                                            int tmp = sc.nextInt();
                                            if (JudgeClass(RealCLASS, tmp)) {
                                                aClass tmp1 = new aClass(tmp);
                                                arr[i].setaClass(tmp1);
                                                System.out.println("success");
                                            } else {
                                                System.out.println("班级不存在");
                                            }
                                            break;
                                        } else {
                                            System.out.println("已存在");
                                            break;
                                        }
                                    }
                                }
                            }
                            //数组满了的话
                            else {
                                System.out.println("请输入要添加学生的学号");
                                String id = sc.next();
                                if (JudgeOnly(arr, id)) {
                                    arr = Creat(arr);
                                    arr[arr.length - 1].setId(id);
                                    System.out.println("请输入要添加学生的名字");
                                    String name = sc.next();
                                    arr[arr.length - 1].setName(name);
                                    System.out.println("请输入要添加学生的班级");
                                    int tmp = sc.nextInt();
                                    if (JudgeClass(RealCLASS, tmp)) {
                                        aClass tmp1 = new aClass(tmp);
                                        arr[arr.length - 1].setaClass(tmp1);
                                        System.out.println("success");
                                    } else {
                                        System.out.println("班级不存在");
                                    }
                                } else {
                                    System.out.println("已存在");
                                }
                                break;
                            }
                            break;
                        }
                        case 2: {
                            System.out.println("输入要删除学生的学号");
                            String input = sc.next();
                            boolean flag1 = true;
                            for (int i = 0; i < arr.length; i++) {
                                if (arr[i] != null) {
                                    if (arr[i].getId().equals(input)) {
                                        arr[i] = null;
                                        System.out.println("删除成功");
                                        flag1 = false;
                                        break;
                                    }
                                }
                            }
                            if (flag1) {
                                System.out.println("不存在的学生");
                            }
                            break;
                        }
                        case 3: {
                            boolean flag1 = true;
                            System.out.println("请输入要修改的学生的学号");
                            String input = sc.next();
                            for (int i = 0; i < arr.length; i++) {
                                if (arr[i] != null) {
                                    if (arr[i].getId().equals(input)) {
                                        flag1 = false;
                                        System.out.println("输入修改后的学号");
                                        String id = sc.next();
                                        if (JudgeOnly(arr, id)) {
                                            arr[i].setId(id);
                                            System.out.println("输入修改后的姓名");
                                            String name = sc.next();
                                            arr[i].setName(name);
                                            System.out.println("请输入要修改后的班级");
                                            int tmp = sc.nextInt();
                                            if (JudgeClass(RealCLASS, tmp)) {
                                                aClass tmp1 = new aClass(tmp);
                                                arr[i].setaClass(tmp1);
                                                System.out.println("success");
                                                break;
                                            } else {
                                                System.out.println("班级不存在");
                                                break;
                                            }
                                        }
                                        else {
                                            System.out.println("修改后的学号已存在");
                                            break;
                                        }
                                    }
                                }
                            }
                            if(flag1){
                                System.out.println("学生不存在");
                            }
                            break;
                        }
                        case 4: {
                            for (int i = 0; i < arr.length; i++) {
                                if (arr[i] != null) {
                                    System.out.println("班级为"+arr[i].getaClass().getaClass()+"班,"+"学号为" + arr[i].getId() + ",姓名为" + arr[i].getName());
                                }
                            }
                            break;
                        }
                        default: {
                            System.out.println("没有该选项");
                            break;
                        }
                    }
                    break;
                }
                case 3: {
                    System.out.println("1.新增小组" + " " + "2.删除小组" + " " + "3.修改小组成员" + " " +
                            "4.查看小组信息");
                    int c = sc.nextInt();
                    switch (c) {
                        case 1: {
                            boolean flag = true;
                            System.out.println("当前有" + CLASS.size() + "组，输入学号添加小组成员");
                            System.out.println("请输入第一个小组成员学号");
                            String first1 = sc.next();
                            for (int i = 0; i < arr.length; i++) {
                                if (arr[i] != null) {
                                    if (arr[i].getId().equals(first1)) {
                                        flag = false;
                                        System.out.println("请输入第二个小组成员学号");
                                        String second1 = sc.next();
                                        for (int j = 0; j < arr.length; j++) {
                                            if (arr[j] != null) {
                                                if (arr[j].getId().equals(second1)) {
                                                    Group tmp = new Group(CLASS.size(), arr[i], arr[j]);
                                                    CLASS.add(tmp);
                                                    System.out.println("添加成功");
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            if (flag) {
                                System.out.println("输入的人不存在");
                            }
                            break;
                        }
                        case 2: {
                            System.out.println("请输入要删除的小组编号");
                            int index = sc.nextInt();
                            if (index < CLASS.size() + 1) {
                                CLASS.remove(index - 1);
                                System.out.println("删除成功");
                            } else {
                                System.out.println("小组不存在");
                            }
                            break;
                        }
                        case 3: {
                            System.out.println("输入要修改的小组编号");
                            int index = sc.nextInt();
                            if (index < CLASS.size() + 1 && index >= 0) {
                                boolean flag = true;
                                System.out.println("请输入第一个小组成员学号");
                                String first1 = sc.next();
                                for (int i = 0; i < arr.length; i++) {
                                    if (arr[i] != null) {
                                        if (arr[i].getId().equals(first1)) {
                                            flag = false;
                                            System.out.println("请输入第二个小组成员学号");
                                            String second1 = sc.next();
                                            for (int j = 0; j < arr.length; j++) {
                                                if (arr[j] != null) {
                                                    if (arr[j].getId().equals(second1)) {
                                                        Group tmp = new Group(CLASS.size(), arr[i], arr[j]);
                                                        CLASS.set(index - 1, tmp);
                                                        System.out.println("修改成功");
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                if (flag) {
                                    System.out.println("输入的人不存在");
                                }
                                break;
                            }
                        }
                        case 4: {
                            for (int i = 0; i < CLASS.size(); i++) {
                                System.out.println("第" + (i + 1) + "组");
                                System.out.println("小组第一个成员是学号" + CLASS.get(i).member1.getId() + "，姓名" + CLASS.get(i).member1.getName());
                                System.out.println("小组第二个成员是学号" + CLASS.get(i).member2.getId() + "，姓名" + CLASS.get(i).member2.getName());
                            }
                            break;
                        }
                    }
                    break;
                }
                case 4: {
                    System.out.println("1.随机抽小组" + " " + "2.小组抽学生" + " " + "3.班级抽学生");
                    int d = sc.nextInt();
                    switch (d) {
                        case 1: {
                            Random r = new Random();
                            int index = r.nextInt(CLASS.size());
                            System.out.println("点到的小组为" + (index + 1) + "组");
                            System.out.println("小组第一个成员是学号" + CLASS.get(index).member1.getId() + "，姓名" + CLASS.get(index).member1.getName());
                            System.out.println("小组第二个成员是学号" + CLASS.get(index).member2.getId() + "，姓名" + CLASS.get(index).member2.getName());
                            break;
                        }
                        case 2: {
                            Random r = new Random();
                            System.out.println("输入要抽取学生的小组编号");
                            int s = sc.nextInt();
                            int index = r.nextInt(2);
                            if (s <= CLASS.size() && s >= 0) {
                                if (index == 0) {
                                    System.out.println("抽到的成员学号" + CLASS.get(s-1).member1.getId() + "，姓名" + CLASS.get(s-1).member1.getName());
                                } else {
                                    System.out.println("抽到的成员学号" + CLASS.get(s-1).member2.getId() + "，姓名" + CLASS.get(s-1).member2.getName());
                                }
                            } else {
                                System.out.println("要抽取的的小组不存在");
                            }
                            break;
                        }
                        case 3: {
                            System.out.println("输入班级");
                            int tmp = sc.nextInt();
                            if (JudgeClass(RealCLASS, tmp) && IsStudent(arr, tmp)) {
                                Random r = new Random();
                                boolean flag = true;
                                while (flag) {
                                    int index = r.nextInt(arr.length);
                                    if (arr[index].getaClass().getaClass() == tmp) {
                                        System.out.println("抽到的学生学号为" + arr[index].getId() + "，姓名" + arr[index].getName());
                                        flag = false;
                                    }
                                }
                            } else {
                                System.out.println("班级不存在或没有添加学生");
                            }
                            break;
                        }
                    }
                    break;
                }
                case 0: {
                    s1 = Exit;
                    break;
                }
                default: {
                    System.out.println("没有选项，重新输入");
                    break;
                }
            }
        } while (s1 != Exit);
    }

    public static int Judge(Student[] arr) {
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null) {
                count++;
            }
        }
        return count;
    }

    public static Student[] Creat(Student[] arr) {
        Student[] arr1 = new Student[arr.length + 1];
        for (int i = 0; i < arr.length; i++) {
            arr1[i] = arr[i];
        }
        arr1[arr1.length - 1] = new Student();
        return arr1;
    }

    public static boolean JudgeOnly(Student[] arr, String input) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null) {
                if (arr[i].getId() != null) {
                    if (arr[i].getId().equals(input)) {
                        return false;
                    }
                }
            }
        }
        return true;
    }

    public static boolean JudgeClass(ArrayList<aClass> c, int a) {
        for (int i = 0; i < c.size(); i++) {
            if (c.get(i).getaClass() == a) {
                return true;
            }
        }
        return false;
    }

    public static boolean IsStudent(Student[] arr, int a) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i].getaClass().getaClass() == a) {
                return true;
            }
        }
        return false;
    }

    public static void DeleteClass(Student[] arr, int a) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i].getaClass().getaClass() == a) {
                arr[i] = null;
            }
        }
    }
}

